package com.wei.myspdemo.controller;

import com.wei.myspdemo.entity.LoginRequest;
import com.wei.myspdemo.entity.User;
import com.wei.myspdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) {
        // 调用userService的方法进行登录验证
        Long userId = userService.authenticate(loginRequest.getUsername(), loginRequest.getPassword());
        User user=null;;
        if (userId!=null) {
            user=userService.findUserById(userId);
            return ResponseEntity.ok(user);
        } else {
            // 登录失败，返回一个错误信息
            String errorMessage = "用户名或密码错误";
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(errorMessage);
        }
    }
}
